package com.tfjybj.itoo.teach.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.ClassEntity;
import com.tfjybj.itoo.teach.model.ClassModel;
import com.tfjybj.itoo.teach.provider.service.ClassService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * ClassController
 * class表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"class表接口"})
@RequestMapping(value = "/class")
@RestController

public class ClassController {

    @Resource
    private ClassService classService;


    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model ClassModel
     * @return 添加的结果（修改，加入判重）
     * @author 杨燕梅
     * @since 2.0.0 2018-12-20 14:41:44
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    @Transactional(rollbackFor = Exception.class)
    public ItooResult create(@RequestBody ClassModel model) {
        if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        if (StringUtils.isEmpty(model.getGrade())){
            return ItooResult.build(ItooResult.FAIL, "grade为空");
        }
        if (StringUtils.isEmpty(model.getProfessionId())){
            return ItooResult.build(ItooResult.FAIL, "professionId为空");
        }
        //判断班级是否重复
        if (!CollectionUtils.isEmpty(classService.queryByCodeAndNameExist(model.getCode(),model.getName()))){
            return ItooResult.build(ItooResult.FAIL, "班级已存在");
        }
        ClassEntity classEntity = new ClassEntity();
        BeanUtils.copyProperties(model, classEntity);
        classService.save(classEntity);
        classService.saveToAuth(classEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        classService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        classService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model ClassModel
     * @return 修改后的结果
     * @author 杨燕梅（修改，加入判重）
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改class")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody ClassModel model) {
        if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        if (StringUtils.isEmpty(model.getGrade())){
            return ItooResult.build(ItooResult.FAIL, "grade为空");
        }
        if (StringUtils.isEmpty(model.getProfessionId())){
            return ItooResult.build(ItooResult.FAIL, "professionId为空");
        }
        ClassEntity classEntity = new ClassEntity();
        BeanUtils.copyProperties(model, classEntity);
        classService.updateById(classEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Class
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        ClassEntity classEntity = classService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", classEntity);
    }

    /**
     * 分页查询所有Class
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有Class")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<ClassEntity> classs = classService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", classs);
    }

    /**
     * 根据行政班名称模糊查询class
     *
     * @param name  行政班名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的class
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据行政班名称模糊查询class", notes = "分页根据行政班名称模糊查询class")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "queryClassByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryClassByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                           @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<ClassEntity> classList = classService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", classList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */
    /**
     *根据专业id查询班级信息集合（带分页）
     * @param professionId 专业id
     * @return 杨燕梅
     * @since 2.0.0 2018-11-11 09:56:25
     */
    @ApiOperation(value = "根据专业id来查询行政班集合（带分页）", notes = "根据专业id来查询行政班集合（带分页）")
    @GetMapping(value = {"queryClassByProfessionIdPageInfo/{pageNo}/{pageSize}/{professionId}"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "页码",dataType = "int",required = true,example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "记录数",dataType = "int",required = true,example = "10"),
            @ApiImplicitParam(name = "professionId", value = "专业id", required = true)
    })
    @ResponseBody
    public ItooResult queryClassByProfessionIdPageInfo(@PathVariable String professionId,
                                                       @PathVariable Integer pageNo,
                                                       @PathVariable Integer pageSize) {
        if (StringUtils.isEmpty(professionId)) {
            return ItooResult.build("1111", "参数为空");
        }
        PageInfo<ClassModel> classList = classService.queryByProfessionIdPageInfo(professionId,pageNo,pageSize);
        if (classList == null || classList.getList().isEmpty()) {
            return ItooResult.build("1111", "查询班级为空！");
        }
        return ItooResult.build("0000", "查询班级成功", classList);
    }

    /**
     *根据专业id查询班级信息集合
     * @param professionId 专业id
     * @return 杨燕梅
     * @since 2.0.0 2018-11-18 08:57:40
     */
    @ApiOperation(value = "根据专业id来查询行政班集合", notes = "根据专业id来查询行政班集合")
    @GetMapping(value = {"queryClassByProfessionId/{professionId}"})
    @ApiImplicitParam(name = "professionId", value = "专业id", required = true)
    @ResponseBody
    public ItooResult queryClassByProfessionId(@PathVariable String professionId) {
        if (StringUtils.isEmpty(professionId)) {
            return ItooResult.build("1111", "参数为空");
        }
        List<ClassModel> classList = classService.queryByProfessionId(professionId);
        //if (classList == null || classList.getList().isEmpty()) {
        //    return ItooResult.build("1111", "查询班级为空！");
        //}
        return ItooResult.build("0000", "查询班级成功", classList);
    }

    /**
     *根据专业id集合查询班级信息集合
     * @param professionIds 专业id集合
     * @return 杨燕梅
     * @since 2.0.0 2018-11-13 19:07:35
     */
    @ApiOperation(value = "根据专业id集合来查询行政班集和", notes = "根据专业id集合来查询行政班集和")
    @PostMapping(value = {"/queryClassByProfessionIds/{professionIds}"})
    @ApiImplicitParam(name = "professionIds", value = "专业id集合",dataType = "List<String>", required = true)
    public ItooResult queryClassByProfessionIds(@RequestBody List<String> professionIds) {
        if (CollectionUtils.isEmpty(professionIds)) {
            return ItooResult.build(ItooResult.FAIL, "专业id集和为空！");
        } else {
            List<ClassModel> classList = classService.queryByProfessionIds(professionIds);
            return ItooResult.build(ItooResult.SUCCESS,"查询行政班集和成功！",classList);
        }
    }

    /**
     *根据行政班id查询专业id
     * @param classId 行政班id集合
     * @return 行政班id
     * @author 田成荣
     * @since 2.0.0 2018-11-13 19:07:35
     */
    @ApiOperation(value = "根据行政班id来查询专业id", notes = "根据行政班id来查询专业id")
    @GetMapping(value = {"/queryClassByProfessionIds/{classId}"})
    public ItooResult queryProfessionIdByClassId(@RequestParam String classId) {
        if (StringUtils.isEmpty(classId)) {
            return ItooResult.build(ItooResult.FAIL, "行政班id为空！");
        } else {
            String  professionId = classService.queryProfessionIdByClassId(classId);
            return ItooResult.build(ItooResult.SUCCESS,"查询专业id成功！",professionId);
        }
    }


    /**
     * 根据年级查询班级信息
     * @param grade
     * @return
     * @author 杨燕梅
     * @since 2.0.0 2018-11-24 15:32:34
     */
    @ApiOperation(value = "根据年级查询班级信息", notes = "根据年级查询班级信息")
    @GetMapping(value = {"queryClassByGrade/{grade}"})
    public ItooResult queryClassByGrade(@RequestParam String grade) {
        List<ClassModel> classList = classService.queryClassByGrade(grade);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", classList);
    }

    /**
     * 根据专业id和年级查询班级信息
     * @author 杨燕梅
     * @since 2.0.0 2018-11-24 21:19:24
     */
    @ApiOperation(value = "根据专业id和年级查询班级信息", notes = "根据专业id和年级查询班级信息")
    @GetMapping(value = {"queryClassByProfessionIdAndGrade/{professionId}/{grade}"})
    public ItooResult queryClassByProfessionIdAndGrade(@PathVariable String professionId,
                                                       @PathVariable String grade) {
        List<ClassModel> classList = classService.queryClassByProfessionIdAndGrade(professionId,grade);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", classList);
    }

    /**
     * 根据班级名称模糊查询某一专业下属班级
     *
     * @param professionId 专业id
     * @param searchContent  行政班名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的class
     * @author 杨燕梅
     * @since 2.0.0 2018-11-29 10:36:13
     */
    @ApiOperation(value = "根据班级名称模糊查询某一专业下属班级", notes = "根据班级名称模糊查询某一专业下属班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "professionId",value = "专业id",dataType = "string",required = true),
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "queryClassByLikeNameAndProfessionId/{professionId}/{pageNo}/{pageSize}")
    public ItooResult queryClassByLikeNameAndProfessionId(@PathVariable String professionId, @PathVariable Integer pageNo,
                                                          @PathVariable Integer pageSize, @RequestParam(required = false, defaultValue = "") String searchContent)
    {
        PageInfo<ClassModel> classList = classService.queryByLikeNameAndProfessionId(professionId, pageNo, pageSize,searchContent);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", classList);
    }

    /**
     * 根据班级ID查询班级详情
     *
     * @param idList 班级ID
     * @return 班级实体集合
     * @author 杨燕梅
     * @since 2.0.0 2018-12-12 21:29:18
     */
    @ApiOperation(value = "根据班级ID查询班级详情", notes = "根据班级ID查询班级详情")
    @GetMapping(value = "/queryClassById/{idList}")
    public ItooResult queryClassById(@ApiParam(name = "idList", value = "班级ID", required = true) @PathVariable List<String> idList) {
        List<ClassModel> classModelList =classService.queryClassById(idList);
        return ItooResult.build(ItooResult.SUCCESS, "查询班级成功!", classModelList);
    }

    /**
     * 根据ID导出班级信息
     *
     * @param response 响应
     * @param idList   班级ID
     * @return 导出结果信息
     * @author 杨燕梅
     * @since 2.0.0 2018-12-12 21:29:22
     */
    @ApiOperation(value = "根据ID导出班级信息", notes = "根据ID导出班级信息")
    @GetMapping(value = {"/exportAll"})
    public ItooResult exportAll(HttpServletResponse response, @RequestParam(required = false, defaultValue = "") List<String> idList) {
        return classService.exportByClassId(response, idList);
    }

    /**
     * 根据条件导出班级
     *
     * @param response     响应
     * @param searchContent    班级名称/班级简称
     * @return 导出结果信息
     * @author 杨燕梅
     * @since 2.0.0 2018-12-23 17:54:38
     */
    @ApiOperation(value = "根据条件导出班级", notes = "根据条件导出班级")
    @GetMapping(value = {"/exportByStrLike/{professionId}"})
    public ItooResult exportByStrLike(HttpServletResponse response, @ApiParam(name = "professionId", value = "专业Id", required = true) @PathVariable String professionId , @RequestParam(required = false, defaultValue = "") String searchContent) {
        return classService.exportByStrLike(response,professionId,searchContent);
    }

    /**
     * 下载班级模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 杨燕梅
     * @since 2.0.0 2018-11-30 10:41:41
     */
    @ApiOperation(value = "下载班级模板，批量导入使用", notes = "下载班级模板，批量导入使用")
    @GetMapping(value = {"/downLoadClassInfoTemplate"})
    public ItooResult downLoadClassInfoTemplate(HttpServletResponse response) {
        if (classService.downLoadTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载班级模板成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载班级模板失败");
        }
    }

    /**
     * 批量导入班级
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 杨燕梅
     * @since 2.0.0 2018-11-30 11:45:32
     */
    @ApiOperation(value = "批量导入班级", notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importClassInfoByTemplate"})
    public ItooResult importClassInfoByTemplate(@RequestParam(value = "file") MultipartFile multipartFile,
                                                HttpServletRequest request, HttpServletResponse response) {
        return classService.importTemplate(multipartFile, request, response);
    }

    /**
     * 根据errorListId导出未成功导入的信息
     *
     * @param response    响应
     * @param errorListId 为成功导入的分组id
     * @return 导出结果
     * @author 杨燕梅
     * @since 1.0.0 2018-12-2 11:55:23
     */
    @ApiOperation(value = "根据errorListId导出未成功导入的信息", notes = "根据errorListId导出未成功导入的信息")
    @GetMapping(value = {"/exportErrorClassById/{errorListId}"})
    public ItooResult exportErrorClassById(HttpServletResponse response,
                                           @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {
        if (classService.exportErrorList(errorListId, response)) {
            return ItooResult.build(ItooResult.SUCCESS, "导出成功");

        } else {
            return ItooResult.build(ItooResult.FAIL, "导出失败");
        }
    }

    /**
     * 查询所有Class
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "查询所有Class", notes = "查询所有Class")
    @GetMapping(value = {"/queryAll"})
    public ItooResult queryAll() {
        List<ClassEntity> classs = classService.queryAll();
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", classs);
    }

    /**
     * 根据班级代码和班级名称查询班级是否已存在
     *
     * @param code   班级代码
     * @param name   班级名称
     * @return 班级信息
     * @author 杨燕梅
     * @since 2.0.0 2018-12-20 09:57:54
     */
    @ApiOperation(value = "根据班级代码和班级名称查询班级是否已存在", notes = "根据班级代码和班级名称查询班级是否已存在")
    @GetMapping(value = {"/queryByCodeAndNameExist/{code}/{name}"})
    public ItooResult queryByCodeAndNameExist(@ApiParam(name = "code", value = "班级代码", required = true) @PathVariable String code,
                                              @ApiParam(name = "name", value = "班级名称", required = true) @PathVariable String name) {
        List<ClassEntity> classList = classService.queryByCodeAndNameExist(code,name);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", classList);
    }
}